﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using DTO;
namespace DAO
{
    public class ChiTietHoaDonDAO
    {
        DataProvider dp;
        public ChiTietHoaDonDAO()
        {
            dp = new DataProvider();
        }
        public bool ThemChiTietHoaDon(ChiTietHoaDonDTO cthddto)
        {
            bool flag = true;
            string sql = "insert into ChiTietHoaDon(mahoadon,masach,soluong,dongia) values ";
            sql += " (" + cthddto.Mahoadon + ", " + cthddto.Masach + ", " + cthddto.Soluong + ", " + cthddto.Dongia + ")";
            if (!dp.ThucHienCauTruyVan(sql)) flag = false;
            return flag;
        }
        public DataTable LayChiTietHoaDon(int mahd)
        {
            DataTable table = new DataTable();
            string sql = "select s.tensach, tl.tentheloai, cthd.soluong, cthd.dongia from Sach s, TheLoai tl, ChiTietHoaDon cthd ";
            sql += " where cthd.mahoadon =" + mahd + " and cthd.masach = s.masach and s.matheloai = tl.matheloai ";
            table = dp.LayBangDuLieu(sql);
            return table;
        }
        public int TinhSLSachDaBan(int masach)
        {
            int sl = 0;
            string sql = "select * from ChiTietHoaDon where masach = " + masach + "";
            DataTable table = new DataTable();
            table = dp.LayBangDuLieu(sql);
            int n = table.Rows.Count;
            for (int i = 0; i < n; i++)
            {
                DataRow dr = table.Rows[i];
                sl += int.Parse(dr[3].ToString());
            }
            return sl;
        }
    }
}
